/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.action.form;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.stream.StreamSource;
import nhadat.dao.KhuVucDAO;
import nhadat.dao.LoaiGiayToDAO;
import nhadat.dao.LoaiNhaDAO;
import nhadat.dao.LoaiTinDAO;
import nhadat.entity.GiaTienSearch;
import nhadat.entity.TblLoaiGiayTo;
import nhadat.entity.TblLoaiNha;
import nhadat.entity.TblLoaiTin;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import nhadat.common.CommonConstant;
import nhadat.dao.TindangDao;
import nhadat.entity.TblTinDang;
import nhadat.xml.XMLmanipulate;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ProcessingInstruction;

/**
 *
 * @author Xe Bay
 */
public class HomePageActionForm extends org.apache.struts.action.ActionForm {

    /* Searching mapping variables */
    private String loaitin;
    private TblLoaiTin[] dsLoaitin;
    private String tinhthanh;
    private String quanhuyen;
    private String[] dsKhuVuc;
    private String[] dsQuanHuyenThuocTinhThanh;
    private String loainha;
    private TblLoaiNha[] dsLoaiNha;
    private String loaiGiayTo;
    private TblLoaiGiayTo[] dsLoaiGiayTo;
    private GiaTienSearch giaTienSearch;
    private String[] dsDvdGiaTien;

    public String getLoaitin() {
        return loaitin;
    }

    public void setLoaitin(String loaitin) {
        this.loaitin = loaitin;
    }

    public TblLoaiTin[] getDsLoaitin() {
        return dsLoaitin;
    }

    public void setDsLoaitin(TblLoaiTin[] dsLoaitin) {
        this.dsLoaitin = dsLoaitin;
    }

    public String[] getDsDvdGiaTien() {
        return dsDvdGiaTien;
    }

    public void setDsDvdGiaTien(String[] dsDvdGiaTien) {
        this.dsDvdGiaTien = dsDvdGiaTien;
    }

    public String[] getDsKhuVuc() {
        return dsKhuVuc;
    }

    public void setDsKhuVuc(String[] dsKhuVuc) {
        this.dsKhuVuc = dsKhuVuc;
    }

    public TblLoaiGiayTo[] getDsLoaiGiayTo() {
        return dsLoaiGiayTo;
    }

    public void setDsLoaiGiayTo(TblLoaiGiayTo[] dsLoaiGiayTo) {
        this.dsLoaiGiayTo = dsLoaiGiayTo;
    }

    public TblLoaiNha[] getDsLoaiNha() {
        return dsLoaiNha;
    }

    public void setDsLoaiNha(TblLoaiNha[] dsLoaiNha) {
        this.dsLoaiNha = dsLoaiNha;
    }

    public GiaTienSearch getGiaTienSearch() {
        return giaTienSearch;
    }

    public void setGiaTienSearch(GiaTienSearch giaTienSearch) {
        this.giaTienSearch = giaTienSearch;
    }

    public String getLoaiGiayTo() {
        return loaiGiayTo;
    }

    public void setLoaiGiayTo(String loaiGiayTo) {
        this.loaiGiayTo = loaiGiayTo;
    }

    public String getLoainha() {
        return loainha;
    }

    public void setLoainha(String loainha) {
        this.loainha = loainha;
    }

    public String[] getDsQuanHuyenThuocTinhThanh() {
        KhuVucDAO khuvucDAO = new KhuVucDAO();
        return khuvucDAO.getAllQuanHuyenByTinhThanh(tinhthanh);
    }

    public void setDsQuanHuyenThuocTinhThanh(String[] dsQuanHuyenThuocTinhThanh) {
        this.dsQuanHuyenThuocTinhThanh = dsQuanHuyenThuocTinhThanh;
    }

    public String getTinhthanh() {
        return tinhthanh;
    }

    public void setTinhthanh(String tinhthanh) {
        this.tinhthanh = tinhthanh;
    }

    public String getQuanhuyen() {
        return quanhuyen;
    }

    public void setQuanhuyen(String quanhuyen) {
        this.quanhuyen = quanhuyen;
    }

    public HomePageActionForm() {
    }

    public void initialSearchInformation() {
        getAllLoaiTin();
        getAllKhuVuc();
        if ("".equals(tinhthanh) && (null != dsKhuVuc)) {
            tinhthanh = dsKhuVuc[0];
        }
        getAllLoaiGiayTo();
        getAllLoaiNha();
        getAllDvdGiaTien();
    }

    public void getAllLoaiTin() {
        LoaiTinDAO loaiTinDAO = new LoaiTinDAO();
        dsLoaitin = loaiTinDAO.getAllLoaiTin();
    }

    public void getAllKhuVuc() {
        KhuVucDAO khuVucDAO = new KhuVucDAO();
        dsKhuVuc = khuVucDAO.getAllTinhThanh();
    }

    public void getAllLoaiNha() {
        LoaiNhaDAO loaiNhaDAO = new LoaiNhaDAO();
        dsLoaiNha = loaiNhaDAO.getAllLoaiNha();
    }

    public void getAllLoaiGiayTo() {
        LoaiGiayToDAO loaiGiayToDAO = new LoaiGiayToDAO();
        dsLoaiGiayTo = loaiGiayToDAO.getAllLoaiGiayTo();
    }

    public void getAllDvdGiaTien() {
        TindangDao tindangDAO = new TindangDao();
        dsDvdGiaTien = tindangDAO.getAllDvdGiaTien();
    }

    public void createHome() {
        try {
            String path = getServlet().getServletContext().getRealPath("/");
//            String path = getServletContext().getRealPath("/");
            TransformerFactory tf = TransformerFactory.newInstance();
            StreamSource xsltFile = new StreamSource(path + "WEB-INF/NhaDatXSL.xsl");
            Transformer trans = tf.newTransformer(xsltFile);
            StreamSource xmlFile = new StreamSource(path + "WEB-INF/NhaDat.xml");
            StreamResult htmlFile = new StreamResult(new FileOutputStream(path + "homepage.jsp"));
            trans.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
            trans.transform(xmlFile, htmlFile);
            System.out.println("Created HTML file");
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(HomePageActionForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(HomePageActionForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(HomePageActionForm.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
